package plantshop.model;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import plantshop.model.plants.Plant;


public class Display implements Iterable<Plant>, Serializable{

	private static final long serialVersionUID = 7458915774005087651L;
	
	private Map<Integer, Plant> plants;
	//Deal with location
	private Location topLeft, bottomRight;
	private int rows, columns; //Not really necessary if you already know what each looks like
	public boolean front;

	public final static int DISPLAY_UNIT_SIZE = 85;
	
	public Display(int size, int x, int y, boolean front, double xscale, double yscale){
		plants = new HashMap<Integer,Plant>(size);
		
		for(int i = 0;i < size; i++)
			plants.put(i, null);
		columns = 2;
		rows = (size/columns);
		topLeft = new Location(x,y);
		if(front)
			bottomRight = new Location((int)(x+columns*DISPLAY_UNIT_SIZE*xscale),(int)(y+rows*DISPLAY_UNIT_SIZE*yscale));
		else
			bottomRight = new Location((int)(x+(columns*(DISPLAY_UNIT_SIZE+10))*xscale),(int)(y+(rows*(DISPLAY_UNIT_SIZE+11))*yscale));
	}
	
	
	
	public void setTopLeft(Location topLeft) {
		this.topLeft = topLeft;
	}



	public void setBottomRight(Location bottomRight) {
		this.bottomRight = bottomRight;
	}



	public void setRows(int rows) {
		this.rows = rows;
	}



	public void setColumns(int columns) {
		this.columns = columns;
	}



	public Location getTopLeft(){
		return topLeft;
	}
	
	public Location getBottomRight(){
		return bottomRight;
	}
	
	public int getRows(){
		return rows;
	}
	
	public int getColumns(){
		return columns;
	}
	
	@Override
	public Iterator<Plant> iterator() {
		return new DisplayIterator(plants);
	}
	
	public Plant addPlant(int i, Plant p){
		plants.put(i, p);
		return p;
	}
	
	public Plant getPlant(int i){
		return plants.get(i);
	}
	
	public Plant removePlant(int i){
		Plant removed = plants.get(i);
		plants.put(i, null);
		return removed;
	}
	
	public void setPlant(int i, Plant p){
		plants.put(i, p);
	}
	
	public Map<Plant, Integer> getPlantCounts(){
		Map<Plant,Integer> counts = new HashMap<Plant, Integer>();
		for(Plant p: this){
			Plant key = p;
			if(counts.containsKey(key))
				counts.put(key, counts.get(key)+1);
			else
				counts.put(key, 1);
		}
		return counts;
	}
	
	public int size(){
		return plants.size();
	}

}
